package Streaming;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.concurrent.Callable;
import java.lang.String;
/**
 * this class handles the registration of
 * a new stream recipient in a separated thread.
 *
 *@version 1.8
 */
public class ClientConnector implements Callable<String> {
	
	private final String pwd;
	private final Socket socket;
	
	public ClientConnector(String pwd, Socket socket){
		this.pwd = pwd;
		this.socket = socket;
	}

	/**
	 * checks the current hard coded password and accepts 
	 * the request as a new stream recipient if the passowrd is correct.
	 * @return the new clients IP as string
	 */
	@Override
	public String call() throws Exception {
		InetAddress clientIP = null;
		String cIP = null;
		
		try {
			DataOutputStream out = new DataOutputStream(socket.getOutputStream());
			DataInputStream in = new DataInputStream(socket.getInputStream());
			
			cIP = (String) in.readUTF();

			
			if(cIP.equals(pwd)){
				clientIP = socket.getInetAddress();
			    
			}else{
				out.writeUTF("False password!!!");
			}
			
			out.flush();
			in.close();
			out.close();
			socket.close();
			System.out.println("connection closed");
			
		} catch (Throwable t) {
			t.printStackTrace();
		}
		
		if(clientIP != null){
			cIP = (String)clientIP.getHostAddress();
		}
		else cIP = "x";
		
		return cIP;
	}

}
